<?php
class CTping extends spController
{
	function __construct(){ // 公用
		parent::__construct(); // 这是必须的
		$this->tpl_title = "产品模块|主题部分";
	}
	
	function index(){ // 这里是首页
		$this->contents = "产品模块首页内容";
		$this->display("produce/main_index.html");
	}
	
	function page()
	{
		 // 前台开启检测功能
		echo '<h2>'.'检测功能'.$i.'</h2>';
		
		$this->start_detection();
		$this->contents = "这里是前台ping控制";
		$this->display("produce/main_page.html");
	}
	
	function start_detection()
	{
		//这里是开启检测功能
		//$this->detection();
		$time = $this->spArgs('time');
		$this->moniter($time);
	}

      //检测
	public function detection($interval)
	{
		if ($interval==""){$interval=60;}
		$site = spClass("psite");		
		$tttc=60;
		$where['active'] = 1;
		$where['p_inter'] = $interval / $tttc;
		$time_id = $where['p_inter']+100000;
		$b = $site->findAll($where);
		
		$runtime = spClass("runtime");			
 	    $runtime->u_time($time_id);
 	    unset($runtime);
		foreach ($b as $i)
		{
            import("detection.php");
            $get_ip = new detection();
            preg_match("/^(http:\/\/)?([^\/]+)/i", "$i[domain]", $matches);
			$host = $matches[2];
			$ip=$get_ip->g_ip($host);
			$p_ing = $get_ip->ping($i[domain]);
			unset($get_ip);
          
			$Model = spClass('ping');
			$data = array
			(				
				'userid'=>$i[userid],
				'siteid'=>$i[siteid],
				'p_time'=>strtotime(date("Y-m-d H:i:s")),
				'Minimum'=>$p_ing[Minimum],
				'Maximum'=>$p_ing[Maximum],					
				'Average'=>$p_ing[Average],				
			); 	
			$Model->create($data);
			unset($Model);
		
			$user = spClass('user');
			$conditions = array
			(
				'userid'=> $i[userid]
			);
			$u = $user->findall($conditions);
			
			$site->create();
			$ssdate= array
			(			
				'Average'=>$p_ing[Average],	
			);
			$wwe['siteid']=$i[siteid];
			$site->update($wwe,$ssdate);
		}
		unset($site);
      	unset($b);
	}
	
	
 /**
    +----------------------------------------------------------
    * 网站链接测试
    +----------------------------------------------------------
    */
    public function http_p($url,$method,$postDate)
    {
        $method ='GET';
        $postDate = '';
        import("WebRequest.php");
        $ht_tp = new WebRequest($url,$method,$postDate);
        
        if (!$c=$ht_tp->getResponse())
        {
        	return 'could not open';
        }
        else 
        {        	
        	return $c->status;
        	$this->display("produce/main_page.html");
        }
    }
    
	function start_ping()
	{
		//这里是ping功能
	}
	

    /**
    +----------------------------------------------------------
    * 循环
    +----------------------------------------------------------
    */
    
	private function moniter($time)
    {    	
    	ignore_user_abort();
        set_time_limit(0);
        $interval= $time;
        do
        {        	
			$this->detection($interval);
			sleep($interval);
		}while(true);
      }
}